"""
Case Type   : 多块预读
Case Name   : 开启多块预读，扫描不同大小的表
Create At   : 2024/11/28
Owner       : @li-xin12345
Description :
    1、建表，重启数据库
    2、扫描空表
    3、扫描数据量不同的表
    4、查看日志中打印的预读块数量
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、预读了设置的块数量
    5、成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SessionTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.tb_name = 't_multi_bulk_read_0009'
        self.pg_log = \
            os.path.join(macro.PG_LOG_PATH, macro.DN_NODE_NAME.split('/')[0])

    def test_multi_bulk_read(self):
        text = '----step1: 建表，重启数据库；expect: 成功----'
        self.log.info(text)
        sql = f"drop table if exists {self.tb_name}_1, {self.tb_name}_2, " \
              f"{self.tb_name}_3 cascade;" \
              f"create table {self.tb_name}_1(c_int int, c_char char(10), " \
              f"c_bool bool default false);" \
              f"create table {self.tb_name}_2(c_int int, c_char char(20), " \
              f"c_bool bool default false) with(orientation=row);" \
              f"create table {self.tb_name}_3(c_int int, c_char char(25), " \
              f"c_bool bool default false) with(orientation=row);" \
              f"insert into {self.tb_name}_2 values(" \
              f"generate_series(1,1000000), " \
              f"'char'||generate_series(1,1000000));" \
              f"insert into {self.tb_name}_3 values(" \
              f"generate_series(1,20000000), " \
              f"'char'||generate_series(1,20000000));"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text}')
        self.assertEqual(res.count(self.constant.CREATE_TABLE_SUCCESS), 3,
                         f'执行失败：{text}')
        self.assertIn('INSERT 0 1000000', res, f'执行失败：{text}')
        self.assertIn('INSERT 0 20000000', res, f'执行失败：{text}')

        msg = self.pri_sh.restart_db_cluster()
        self.log.info(msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '----step2: 扫描空表；expect: 成功----'
        self.log.info(text)
        sql = f"set heap_bulk_read_size = 8;" \
              f"show heap_bulk_read_size;" \
              f"select * from t_multi_bulk_read_0009_1;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res, f'执行失败：{text}')
        self.assertIn('64kB', res, f'执行失败：{text}')
        self.assertIn('0 rows', res, f'执行失败：{text}')

        text = '----step3: 扫描数据量不同的表；expect: 成功----'
        self.log.info(text)
        sql = f"set heap_bulk_read_size = 32;" \
              f"show heap_bulk_read_size;" \
              f"select count(*) from t_multi_bulk_read_0009_2;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res, f'执行失败：{text}')
        self.assertIn('256kB', res, f'执行失败：{text}')
        self.assertIn('1000000', res, f'执行失败：{text}')

        sql = f"set heap_bulk_read_size = 16;" \
              f"show heap_bulk_read_size;" \
              f"select count(*) from t_multi_bulk_read_0009_3;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, res, f'执行失败：{text}')
        self.assertIn('128kB', res, f'执行失败：{text}')
        self.assertIn('20000000', res, f'执行失败：{text}')

        text = '----step4: 查看日志中打印的预读块数量；expect: 预读了设置的块数量----'
        self.log.info(text)
        cmd = f"cd {self.pg_log};" \
              f"ls -t | head -1 | awk '{{{{print $1}}}}' | " \
              f"xargs cat | tail -30 | " \
              f"grep 'End of pre-Read, the max blocks batch is'"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user_node, cmd)
        self.log.info(res)
        self.assertIsNotNone(res, f'执行失败：{text}')

    def tearDown(self):
        text = '----step5: 清理环境;expect:成功----'
        self.log.info(text)
        sql = f"drop table if exists {self.tb_name}_1, {self.tb_name}_2, " \
              f"{self.tb_name}_3 cascade;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')